﻿using System;
using System.Drawing.Printing;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;
using FastReport.Export.Text;

namespace SksApp
{
    public enum PrintingTypes
    {
        Rapor, Fis, Fatura, Iade
    }
    public class DotMatrixPrint
    {
        static string ConvertToHex(string asciiString)
        {
            string hex = "";
            foreach (char c in asciiString)
            {
                int tmp = c;
                hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
            }
            return hex;
        }

        static string GetDefaultPrinter()
        {
            PrinterSettings settings = new PrinterSettings();
            foreach (string printer in PrinterSettings.InstalledPrinters)
            {
                settings.PrinterName = printer;
                if (settings.IsDefaultPrinter)
                    return printer;
            }
            return string.Empty;
        }

        public static void Print(BindingSource binding, PrintingTypes evrak)
        {
            string printName = "";
            ReportTypeOption op = ReportTypeOption.Fatura;
            switch (evrak)
            {
                case PrintingTypes.Rapor:
                    printName = Settings.Instance.GetValue(Information.RaporYazicisi);
                    op = ReportTypeOption.ZRapor;
                    break;
                case PrintingTypes.Fis:
                    printName = Settings.Instance.GetValue(Information.FisYazicisi);
                    op = ReportTypeOption.Fis;
                    break;
                case PrintingTypes.Fatura:
                    printName = Settings.Instance.GetValue(Information.FaturaYazicisi);
                    op = ReportTypeOption.Fatura;
                    break;
                case PrintingTypes.Iade:
                    printName = Settings.Instance.GetValue(Information.IadeYazicisi);
                    op = ReportTypeOption.Iade;
                    break;
            }

            if (string.IsNullOrEmpty(printName))
            {
                if (System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count > 0)
                {
                    printName = GetDefaultPrinter();
                }
            }


            if (!string.IsNullOrEmpty(printName))
            {
                using (FastReport.Report report = ReportTemplate.GetReport(op))
                {
                    if (Settings.Instance.GetValue(Information.YazdirmaModu) == "Dos")
                    {
                        DosPrint(report, binding, printName);
                    }
                    else
                    {
                        WindowsPrint(report, binding, printName);
                    }
                }
            }
            else
            {
                MessageBox.Show("Tanımlı Yazıcı bulunamadı !");
            }
        }


        private static void WindowsPrint(FastReport.Report report, System.Windows.Forms.BindingSource binding, string printName)
        {
            report.RegisterData(binding, "Invoice", 10);
            report.GetDataSource("Invoice").Enabled = true;
            report.PrintSettings.Printer = printName;
            report.PrintSettings.ShowDialog = false;
            report.Prepare();
            report.Print();
        }
        private static void DosPrint(FastReport.Report report, System.Windows.Forms.BindingSource binding, string printName)
        {
            report.RegisterData(binding, "Invoice", 10);
            report.GetDataSource("Invoice").Enabled = true;
            report.Prepare();
            TextExport export = new TextExport();
            TextExport textExport = new TextExport();
            textExport.PrinterTypes = export.PrinterTypes;
            textExport.Copies = export.Copies;
            textExport.PageBreaks = true;
            textExport.EmptyLines = true;
            textExport.Frames = true;
            textExport.TextFrames = false;
            textExport.DataOnly = false;
            textExport.Encoding = Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage);
            //textExport.Encoding = Encoding.GetEncoding("ibm857");
            textExport.ScaleX = (float)1.20f;
            textExport.ScaleY = (float)0.87f;
            textExport.PrintAfterExport = true;
            textExport.OpenAfterExport = false;
            textExport.AvoidDataLoss = false;
            using (MemoryStream stream = new MemoryStream())
            {
                report.Prepare();
                report.Refresh();
                textExport.Export(report, stream);
                byte[] dizi = new byte[7];
                dizi[0] = 0x1b;
                dizi[1] = 0x69;

                dizi[2] = 27;
                dizi[3] = 112;
                dizi[4] = 0;
                dizi[5] = 25;
                dizi[6] = 250;

                stream.Write(dizi, 0, 7);
                FastReport.Export.Text.TextExportPrint.PrintStream(printName, "Invoice", 1, stream);
            }
        }
    }
}
